(function(w){


	var login = {

		psReg:/[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
		cookieDate:function(){return new Date(new Date().getTime() + 86400000)},
		cookieunDate:function(){return new Date(new Date().getTime()-10000)},
		//注册审核逻辑
		zhuce:function(){

			var self = this

			var formdata = this.getformdata()

			for(key in formdata){

				if(formdata[key] == ''){
					alert('不可有空值')
					return
				}
			}
			var psReg = this.psReg

			var emr = psReg.test(formdata['email'])

			if(!emr){

				alert('邮箱不合格')

				return
			}

			var pasr =  formdata['password'] == formdata['nextPassword']

			if(!pasr){

				alert('两次输入密码不一致')

				return
			}

			var reg =/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
	        var re = new RegExp(reg)
	        if (!re.test(formdata['password'])) { 

	        	alert('密码过于简单至少包含大小写字母和数字不能少于六位')
	            return
	        }


			this.wxUsernameAndEmailyanzheng('getuserEmail',{email:formdata['email']},function(len){

				if(len>0){

					alert('邮箱已经被注册')

					return

				}else{


					self.wxUsernameAndEmailyanzheng('getuserUserName',{userName:formdata['user']},function(len){


						if(len>0){

							alert('用户名已经被注册')

							return

						}else{


							wxCloud.cloud().callFunction({
								name:"addUsers",
								data:formdata,
								success:function(res){
									console.log(res)
									var id = res.result._id
									self.setCookie(id)
									alert('账号注册成功')
									window.location.href="../index-bootstarp.html"

								},
								fail:function(error){
									console.log(error)
								}
							})
						}
					})
				}
			})

		},
		getformdata:function (){

			var formeles = $("#logform")[0].elements

			var res = {}

			for(var i = 0 ; i < formeles.length ; i++){

				res[formeles[i].name]=formeles[i].value
				
			}

			return res

		},
		wxUsernameAndEmailyanzheng:function(name,whereObj,fun){

			wxCloud.cloud().callFunction({
				name:name,
				data:whereObj,
				success:function(res){
					
					if(res.result == null){
						fun(0)
					}else{

						var len =  res.result.data.length

						fun(len)
					}
					
				},
				fail:function(error){
					console.log(error)
				}
			})
		},
		loginYanZheng:function(){

			var self = this
			
			var formdata = this.getformdata()

			for(key in formdata){
				if(key == "email" || key=="password"){
					if(formdata[key] == ''){
						alert('不可有空值')
						return
					}
				}
			}

			var cloudFun = "getUserLoginYanZheng"

			wxCloud.cloud().callFunction({
				name:cloudFun,
				data:{email:formdata['email'],password:formdata['password']},
				success:function(res){
					console.log(res)
					var len = res.result.data.length

					if(len<1){
						alert('账号或密码错误')
					}else{

						var id = res.result.data[0]._id

						self.setCookie(id)

						window.location.href= "../index-bootstarp.html"

					}
				},
				fail:function(erro){
					console.log(erro)
				}
			})



		},
		setCookie:function(id){
			document.cookie="sgbId=" + id + "; expires=" + this.cookieDate() +"; path=/";
		},
		unsetCookie:function(id){
			document.cookie="sgbId=" + id + "; expires=" + this.cookieunDate() +"; path=/";
		}
	}
	

	window.login = login


})(window)


